/***************************************** 
 * Desc: UGUI面板Inspector(细分功能待定)
*****************************************/

using Common.UGUI.View;
using System;
using UnityEditor;
using UnityEngine;

namespace Common.UserEditor.UGUIViewSpace {

	[CustomEditor(typeof(UGUIPanel))]
	[CanEditMultipleObjects]
	public class UGUIPanelInspector : Editor {

		public override void OnInspectorGUI() {

            UnityEngine.Object[] objects = targets;
			int len = objects.Length;
			UGUIPanel[] panels = new UGUIPanel[len];
			for(int i=0; i< len; ++i) {
				panels[i] = (UGUIPanel)objects[i];
            }

			ShowUGUIPanel(panels, base.OnInspectorGUI);
		}

		#region 自定义子面板

		private static void ShowIsInteractable(UGUIPanel[] panels) {

			UGUIPanel panel = panels[0];

			//GUILayout.Space(UnityEditorCommonConfig.GUILayoutGapSpace);
			GUILayout.BeginHorizontal();
			GUILayout.Label("IsInteractable:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));
			bool interactable = GUILayout.Toggle(panel.IsInteractable, "", UGUIViewInspector.UGUIViewStatusToggle);
			foreach (var onePanel in panels) {
				onePanel.SetIsInteractable(interactable);
			}
			GUILayout.EndHorizontal();
		}

		private static readonly float _ShowCallbackNamePopupLength = 300;

		private static void ShowOnPointerDownCallbackNames(UGUIPanel[] uguiPanels) {

		//	GUILayout.Space(UnityEditorCommonConfig.GUILayoutGapSpace);

			GUILayout.BeginVertical();

			GUILayout.Label("PointerDownCallbacks:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			GUILayout.BeginHorizontal();
			GUILayout.Space(UGUIViewInspector.UITabLength);

			if (uguiPanels.Length > 1) {

				GUILayout.Label("Only select one GameObject can show it's PointerDownCallbacks.", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			} else {

				UGUIPanel uguiPanel = uguiPanels[0];

				string[] callbackNames = uguiPanel.PointerDownCallbackNameArray;
				if (callbackNames.Length == 0) {

					callbackNames = new string[1] { "None" };
				}

				EditorGUILayout.Popup(0, callbackNames, GUILayout.Width(_ShowCallbackNamePopupLength));
			}

			GUILayout.EndHorizontal();
			GUILayout.EndVertical();
        }

		private static void ShowOnPointerDragCallbackNames(UGUIPanel[] uguiPanels) {

		//	GUILayout.Space(UnityEditorCommonConfig.GUILayoutGapSpace);

			GUILayout.BeginVertical();

			GUILayout.Label("PointerDragCallbacks:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			GUILayout.BeginHorizontal();
			GUILayout.Space(UGUIViewInspector.UITabLength);

			if (uguiPanels.Length > 1) {

				GUILayout.Label("Only select one GameObject can show it's PointerDragCallbacks.", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			} else {

				UGUIPanel uguiPanel = uguiPanels[0];

				string[] callbackNames = uguiPanel.PointerDragCallbackNameArray;
				if (callbackNames.Length == 0) {

					callbackNames = new string[1] { "None" };
				}
				EditorGUILayout.Popup(0, callbackNames, GUILayout.Width(_ShowCallbackNamePopupLength));
			}

			GUILayout.EndHorizontal();
			GUILayout.EndVertical();
		}

		private static void ShowOnPointerUpCallbackNames(UGUIPanel[] uguiPanels) {

			//GUILayout.Space(UnityEditorCommonConfig.GUILayoutGapSpace);

			GUILayout.BeginVertical();

			GUILayout.Label("PointerUpCallbacks:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			GUILayout.BeginHorizontal();
			GUILayout.Space(UGUIViewInspector.UITabLength);

			if (uguiPanels.Length > 1) {

				GUILayout.Label("Only select one GameObject can show it's PointerUpCallbacks.", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			} else {

				UGUIPanel uguiPanel = uguiPanels[0];

				string[] callbackNames = uguiPanel.PointerUpCallbackNameArray;
				if (callbackNames.Length == 0) {

					callbackNames = new string[1] { "None" };
				}
				EditorGUILayout.Popup(0, callbackNames, GUILayout.Width(_ShowCallbackNamePopupLength));
			}

			GUILayout.EndHorizontal();

			GUILayout.EndVertical();
		}

		private static void ShowOnPointerClickCallbackNames(UGUIPanel[] uguiPanels) {

			//GUILayout.Space(UnityEditorCommonConfig.GUILayoutGapSpace);

			GUILayout.BeginVertical();

			GUILayout.Label("PointerClickCallbacks:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			GUILayout.BeginHorizontal();
			GUILayout.Space(UGUIViewInspector.UITabLength);

			if (uguiPanels.Length > 1) {

				GUILayout.Label("Only select one GameObject can show it's PointerClickCallbacks.", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			} else {

				UGUIPanel uguiPanel = uguiPanels[0];

				string[] callbackNames = uguiPanel.PointerClickCallbackNameArray;
				if (callbackNames.Length == 0) {

					callbackNames = new string[1] { "None" };
				}
				EditorGUILayout.Popup(0, callbackNames, GUILayout.Width(_ShowCallbackNamePopupLength));
			}

			GUILayout.EndHorizontal();
			GUILayout.EndVertical();
		}

		private static void ShowOnPointerEnterCallbackNames(UGUIPanel[] uguiPanels) {

			//GUILayout.Space(UnityEditorCommonConfig.GUILayoutGapSpace);

			GUILayout.BeginVertical();

			GUILayout.Label("PointerEnterCallbacks:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			GUILayout.BeginHorizontal();
			GUILayout.Space(UGUIViewInspector.UITabLength);

			if (uguiPanels.Length > 1) {

				GUILayout.Label("Only select one GameObject can show it's PointerEnterCallbacks.", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			} else {

				UGUIPanel uguiPanel = uguiPanels[0];

				string[] callbackNames = uguiPanel.PointerEnterCallbackNameArray;
				if (callbackNames.Length == 0) {

					callbackNames = new string[1] { "None" };
				}
				EditorGUILayout.Popup(0, callbackNames, GUILayout.Width(_ShowCallbackNamePopupLength));
			
			}

			GUILayout.EndHorizontal();
			GUILayout.EndVertical();
		}

		private static void ShowOnPointerExitCallbackNames(UGUIPanel[] uguiPanels) {

		//	GUILayout.Space(UnityEditorCommonConfig.GUILayoutGapSpace);

			GUILayout.BeginVertical();

			GUILayout.Label("PointerDownCallbacks:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			GUILayout.BeginHorizontal();
			GUILayout.Space(UGUIViewInspector.UITabLength);

			if (uguiPanels.Length > 1) {

				GUILayout.Label("Only select one GameObject can show it's PointerDownCallbacks.", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			} else {

				UGUIPanel uguiPanel = uguiPanels[0];

				string[] callbackNames = uguiPanel.PointerExitClickCallbackNameArray;
				if (callbackNames.Length == 0) {

					callbackNames = new string[1] { "None" };
				}
				EditorGUILayout.Popup(0, callbackNames, GUILayout.Width(_ShowCallbackNamePopupLength));
			}

			GUILayout.EndHorizontal();
			GUILayout.EndVertical();
		}

		#endregion

		#region 通用自定义面板接口

		public static void ShowUGUIPanel(UGUIPanel[] uguiPanels, Action originalInspectorFunc, bool ifShowCallbackNames = true) {

			UGUIViewInspector.ShowUGUIView(uguiPanels, originalInspectorFunc, new UGUIViewOnValueCallback());
			ShowIsInteractable(uguiPanels);
			if (ifShowCallbackNames) {
				ShowOnPointerDownCallbackNames(uguiPanels);
				ShowOnPointerDragCallbackNames(uguiPanels);
				ShowOnPointerUpCallbackNames(uguiPanels);
				ShowOnPointerClickCallbackNames(uguiPanels);
				ShowOnPointerEnterCallbackNames(uguiPanels);
				ShowOnPointerExitCallbackNames(uguiPanels);
			}
		}

		#endregion
    }
}
